package edu.washington.cs.workflow.matching;

import com.ibm.wala.ipa.callgraph.CGNode;

import edu.washington.cs.workflow.analysis.AnalysisBuilder;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.util.Globals;
import edu.washington.cs.workflow.util.Utils;
import junit.framework.TestCase;

//3 is OK
public class TestMatchingFreemind extends TestCase {
	
	public static String oldDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.71\\freemind-0.71.jar";
	
	public static String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\original-jars\\freemind-0.8.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\forms-1.0.5.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\commons-lang-2.0.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\dom.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\jax-qname.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\jaxb-api.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\jaxb-impl.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\jaxb-libs.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\namespace.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\relaxngDatatype.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\sax.jar"
	    + Globals.pathSep + "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\freemind-0.8\\ant\\lib\\xsdlib.jar"
	    ;

	public static String main = "Lfreemind/main/FreeMind";
	
	public static String[] pkgs = new String[]{"freemind."};
	
	public void testFindMethodInFreemind() {
		
		AnalysisBuilder oldBuilder = new AnalysisBuilder(oldDir, main, pkgs);
		oldBuilder.setCGType(CG.RTA);
		oldBuilder.setUIFramework(UIFramework.SWING);
		oldBuilder.buildAnalysis();
		
		AnalysisBuilder newBuilder = new AnalysisBuilder(newDir, main, pkgs);
		newBuilder.setCGType(CG.RTA);
		newBuilder.setUIFramework(UIFramework.SWING);
		newBuilder.buildAnalysis();
		
		//workflow 1
		String sig1 = "freemind.modes.mindmapmode.MindMapController$ExportToHTMLAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		//workflow 2
		String sig2 = "";
		//workflow 3
		String sig31 = "freemind.controller.Controller$NavigationNextMapAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		String sig32 = "freemind.controller.Controller$MapModuleManager.nextMapModule()V";
		//workflow 4
		String sig4 = "freemind.modes.mindmapmode.MindMapController$ExportBranchToHTMLAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		//workflow 5
		String sig5 = "freemind.modes.ControllerAdapter.toggleFolded()V";
		String sig51 = "freemind.modes.ControllerAdapter$ToggleFoldedAction.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		//workflow 6
		String sig6 = ""; //hard to repair
		
		ExecutionTraceMatcher matcher = new ExecutionTraceMatcher(oldBuilder, newBuilder);
		
		System.out.println("\n1st workflow");
		CGNode node1 = oldBuilder.findCGNodeBySignature(sig1);
		System.out.println(node1);
		Utils.checkNotNull(node1);
		CGNode matchedNode1 = matcher.findMatchedMethodByName(node1);
		System.out.println("Matched node 1: " + matchedNode1);
		Utils.checkNotNull(matchedNode1);
		CGNode counterpart1 = matcher.findCounterpart(node1);
		Utils.checkNotNull(counterpart1);
		
		System.out.println("\n2nd workflow");
//		CGNode node2 = oldBuilder.findCGNodeBySignature(sig2);
//		System.out.println(node2);
		
		System.out.println("\n3rd workflow");
		CGNode node31 = oldBuilder.findCGNodeBySignature(sig31);
		System.out.println(node31);
		Utils.checkNotNull(node31);
		CGNode node32 = oldBuilder.findCGNodeBySignature(sig32);
		System.out.println(node32);
		Utils.checkNotNull(node32);
		CGNode matchedNode31 = matcher.findMatchedMethodByName(node31);
		System.out.println("Matched node 31: " + matchedNode31);
		CGNode matchedNode32 = matcher.findMatchedMethodByName(node32);
		System.out.println("Matched node 32: " + matchedNode32);
		matchedNode32 = matcher.findSameMethodByName(node32);
		System.out.println("Name matched node 32: " + matchedNode32);
		Utils.checkNotNull(matchedNode32);
		CGNode counterpart31 = matcher.findCounterpart(node31);
		CGNode counterpart32 = matcher.findCounterpart(node32);
		Utils.checkNotNull(counterpart31);
		Utils.checkNotNull(counterpart32);
		
		System.out.println("\n4th workflow");
		CGNode node4 = oldBuilder.findCGNodeBySignature(sig4);
		System.out.println(node4);
		Utils.checkNotNull(node4);
		CGNode matchedNode4 = matcher.findMatchedMethodByName(node4);
		Utils.checkNotNull(matchedNode4);
		CGNode counterpart4 = matcher.findCounterpart(node4);
		Utils.checkNotNull(counterpart4);
		
		System.out.println("\n5th workflow");
		CGNode node5 = oldBuilder.findCGNodeBySignature(sig5);
		System.out.println(node5);
		Utils.checkNotNull(node5);
		CGNode node51 = oldBuilder.findCGNodeBySignature(sig51);
		System.out.println(node51);
		Utils.checkNotNull(node51);
		CGNode matchedNode5 = matcher.findMatchedMethodByName(node5);
		System.out.println("Matched node 5: " + matchedNode5);
		CGNode matchedNode51 = matcher.findMatchedMethodByName(node51);
		System.out.println("Matched node 51: " + matchedNode51);
		matchedNode51 = matcher.findSameMethodByName(node51);
		System.out.println("Name matched node 51: " + matchedNode51);
		Utils.checkNotNull(matchedNode51);
		CGNode counterpart5 = matcher.findCounterpart(node5);
		Utils.checkNotNull(counterpart5);
		CGNode counterpart51 = matcher.findCounterpart(node51);
		Utils.checkNotNull(counterpart51);
		
		System.out.println("\n6th workflow");
	}
	
}